#!/usr/bin/env bash

# Script to list bookmarks from Chromium profile.
# Should also work with Chromium based browsers like Iridium, Brave etc.
# by Adnan Shameem (adnan360); License: MIT (Expat)
#
# Usage:
# - Change the 'profile_dir=' line below to match your browser/setup
# - Run the script to get bookmarks: ./chromium-bookmarks.sh

# Prints bookmarks when provided a Chromium/based browser profile directory.
# Params:
#   $1: Profile dir, where "Bookmarks" file lives
function extract_chromium_bookmarks() {

	if [ -f "${1}/Bookmarks" ]; then
		# This brings in name (title), type and url - separated by a line of "--"
		grep -EB2 '"url":' "$1/Bookmarks" | \
		# We don't need the type line
		sed '/"type":/d' | \
		# We don't need the '--' line either
		sed '/^--$/d' | \
		# Join lines into one single line, so that it can be processed by grep
		sed -e ':a' -e 'N' -e '$!ba' -e 's/\n//g' | \
		# Find the title and url and show them on output
		sed -n -e 's|\s*"name": "\([^"]*\)"[^"]*"url": "\([^"]*\)"|\1 -> \2\n|pg'
	else
		echo "Error: The bookmark file ${1}/Bookmarks doesn't exist"; exit 4284
	fi

}

# Holds the profile directory. The directory where the "Bookmarks" file lives.
# Set it to your Chromium/based browser's profile directory.
# For example:
# - Iridium: "$HOME/.config/iridium/Default/"
# - Brave: "$HOME/.config/BraveSoftware/Brave-Browser/Default/"
# - Chromium: "$HOME/.config/chromium/Default/"
profile_dir="$HOME/.config/iridium/Default/"

# Finally, show the bookmarks!
extract_chromium_bookmarks "$profile_dir"
